﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Light.Utility
{
    /// <summary>
    ///输入验证类
    /// </summary>
    public class InputValidator
    {
        /// <summary>
        /// 判断输入的值为数字
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public static bool IsNumeric(string strInput)
        {
            Regex reg = new Regex(@"(^\d+$)");
            if (reg.IsMatch(strInput))
            {
                int value = 0;
                if (int.TryParse(strInput, out value))
                    return true;
                return false;
            }
            else
                return false;
        }

        /// <summary>
        /// 校验电子邮箱
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool IsEmail(string email)
        {

            Regex reg = new Regex(@"^([\w\-]+\.)*[\w\-]+@([\w\-]+\.)+([\w\-]{2,3})$");
            if (!reg.IsMatch(email))
            {
                return false;
            }

            return true;

        }

        /// <summary>
        /// 验证身份证号码
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public static bool IsIDCardNo(string strInput)
        {
            //验证18位身份证号码
            string pattern = @"(^[1-9][0-9]{5}((19[0-9]{2})|(200[0-9])|2011)(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])[0-9]{3}[0-9xX]$)";
            Regex reg = new Regex(pattern);
            if (reg.IsMatch(strInput))
            {
                return true;
            }
            //验证15位身份证号码
            pattern = @"(^[1-9][0-9]{5}([0-9]{2})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])[0-9]{3}$)";
            reg = new Regex(pattern);
            if (reg.IsMatch(strInput))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 是否包含SQL注入字符
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public static bool SqlInjected(string strInput)
        {
            string pattern = @"(OR|LIKE|UPDATE|DELETE|SELECT|INSERT|SET|DROP|TRUNCATE|CREATE|>|<|=|--|//|'|\%$)";
            //string pattern = @"((OR)|(LIKE)|%|(--)|(//)|(UPDATE)|(DELETE)|(SELECT)|(INSERT)|(SET)|(DROP)|(TRUNCATE)|(CREATE)|(>)|(<)|(')$)";
            Regex reg = new Regex(pattern);
            if (reg.IsMatch(strInput))
            {
                return true;
            }
            return false;
        }


        ///// <summary>
        ///// 手机号码文本框格式化
        ///// </summary>
        ///// <param name="e"></param>
        ///// <param name="text"></param>
        //public static void TxtMobilePhoneKeyPressHandle(KeyPressEventArgs e, string text)
        //{

        //    //数字,退格,空格
        //    if (e.KeyChar <= 57 && e.KeyChar >= 48 || e.KeyChar == 8 || e.KeyChar == 32)

        //        e.Handled = false;
        //    else
        //        e.Handled = true;

        //    if (text.Length == 2
        //        || text.Length == 7) //158 1234 1234
        //    {
        //        SendKeys.Send(" ");
        //    }
        //    else if (text.Length > 12 && e.KeyChar != 8)
        //    {
        //        e.Handled = true;

        //    }
        //}

        ///// <summary>
        ///// 固定电话(不含区号)文本框格式化  1234 567
        ///// </summary>
        ///// <param name="e"></param>
        ///// <param name="text"></param>
        //public static void TxtTelPhoneKeyPressHandle(KeyPressEventArgs e, string text)
        //{


        //    //数字,退格,空格
        //    if (e.KeyChar <= 57 && e.KeyChar >= 48 || e.KeyChar == 8 || e.KeyChar == 32)

        //        e.Handled = false;
        //    else
        //        e.Handled = true;

        //    if (text.Length == 3
        //        || text.Length == 8
        //        || text.Length == 13) // 1234 123
        //    {
        //        SendKeys.Send(" ");
        //    }
        //    else if (text.Length > 12 && e.KeyChar != 8)
        //    {
        //        e.Handled = true;

        //    }
        //}


        /// <summary>
        /// 检验URL
        /// </summary>
        /// <param name="URL"></param>
        /// <returns></returns>
        public static bool IsURL(string URL)
        {
            string pattern = "((http|https|ftp):(////|////)((/w)+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(((//[/~]*|//[/~]*)(/w)+)|[.](/w)+)*(((([?](/w)+){1}[=]*))*((/w)+){1}([/&](/w)+[/=](/w)+)*)*)$";
            Regex reg = new Regex(pattern);
            if (reg.IsMatch(URL))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 判断电话格式是否正确
        /// </summary>
        /// <param name="tel"></param>
        /// <returns></returns>
        public static bool IsTel(string tel)
        {
            //(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)$
           // string pattern = @"(^\d{3}-)?\d{8}|(\d{4}-)(\d{7}$)";

          //  string pattern = @"[0-9]{3,4}[0-9]{7,8}|[0-9]{12,13}";
            //string pattern =@"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)$";
            string pattern = @"((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}|\d{5}))$)";

            Regex reg = new Regex(pattern);
            if (reg.IsMatch(tel))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 是否为手机号码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsMobilePhone(string value)
        {
            string patten = @"(^1[358][0-9]{9}$)";
            Regex reg = new Regex(patten);
            if (reg.IsMatch(value)) return true;
            return false;
        }


        /// <summary>
        /// 判断输入的值是否为 金额
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public static bool IsMoney(string strInput)
        {
            string pattern = @"(^(\d+)(\.{0,1})(\d*)$)";
            Regex reg = new Regex(pattern);
            if (reg.IsMatch(strInput))
                return true;
            else
                return false;
        }

        public static bool IsPostCode(string value)
        {
            string pattern = @"^[1-9]\d{5}$";
            Regex reg = new Regex(pattern);
            if (reg.IsMatch(value))
                return true;
            else
                return false;
        }
    }
}
